package ControlLayer;


import java.util.ArrayList;
import DBLayer.DBConnection;
import DBLayer.DBStore;
import DBLayer.IFDBStore;
import ModelLayer.Store;

public class StoreCtr {
	
	public StoreCtr()
	{
	}
	
	//This method returns all Stores in an Arraylist.
	public ArrayList<Store> listAllStores()
	{
		IFDBStore dbSto = new DBStore();
		ArrayList<Store> allSto = new ArrayList<Store>();
		allSto = dbSto.listAllStores(false);
		return allSto;
	}
	
	//This method search for a store on phoneNumber.
	public Store findStore(int phoneNumber)
	{
		IFDBStore dbSto = new DBStore();
		return dbSto.findStore(phoneNumber, false);
	}
	
	//This method create a new store in DBStore with phoneNumber, adress, zipCode and email. 
	public void insertStore(int phoneNumber, String adress, int zipCode, String email)
	{
		Store stoObj = new Store();
		stoObj.setPhoneNumber(phoneNumber);
		stoObj.setAdress(adress);
		stoObj.setZipCode(zipCode);
		stoObj.setEmail(email);
		
		try{
			DBConnection.startTransaction();
			DBStore dbsto = new DBStore();
			dbsto.insertStore(stoObj);
			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	//This method delete a store in DBStore on phoneNumber.
	public void deleteStore(int phoneNumber)
	{
		DBStore dbstore = new DBStore();
		dbstore.deleteStore(phoneNumber);	
	}
	
	//This method update a store with the new values phoneNumber, adress, zipCode and email.
	public int updateStore(int phoneNumber, String adress, int zipCode, String email)
	{
		IFDBStore dbSto = new DBStore();
		Store stoObj = new Store();
		
		stoObj.setPhoneNumber(phoneNumber);
		stoObj.setAdress(adress);
		stoObj.setZipCode(zipCode);
		stoObj.setEmail(email);
		return dbSto.updateStore(stoObj);
	}

}
